<?php
//+--------------
//|实现IModel
//+--------------
class Model {
	public  $sql;
	public  $TableName;
	
	/**
	 * 插入数据
	 * @see IModel::Create()
	 */
	function Create($DataArray){		
		$find=null;
		$values=null;
		
		if (@count($DataArray)){
		     foreach ($DataArray as $key=>$value) {
   	  		
   	  		 $find.=",".$key;
   	  		 
   	  		 if (is_string($value)){
   	  		 	$value="'$value'";
   	  		 }
   	  		 
   	  		 $values.=",".$value;
   	  		
   	  	    }
   	  	    
   	  	    $find=trim($find,",");
   	  	    
   	  	    $values=trim($values,",");
   	  	    
   	  	    $this->sql="INSERT INTO ".$this->TableName." ($find)"."VALUES($values)";	
		}
		
		new PDO_DB_Conn();
		
		if ($pdo=PDO_DB_Conn::$PDOs) {
			 
   	  	    $rs=$pdo->prepare($this->sql);
   	  	       	  	
   	  	    return $rs->execute(); 
		}else{
			return false;
		}
		
	}
	/**
	 * 更新的数据
	 * @see IModel::Update()
	 */
	function Update($DataArray){
		$where=null;
		$find=null;
		if (@count($DataArray)){
			
			foreach ($DataArray["set"] as $key=>$value) {
				
				if (is_string($value)){
					$value="'$value'";
				}
				
				$find.=",".$key."=".$value;
			}
			
			$sets=trim($find,",");
			
			$sets="set ".$sets;
			
			$where=" where ".$DataArray["where"][0];		
			
			$this->sql="Update ".$this->TableName." $sets"." $where ";
		}
		
		new PDO_DB_Conn();
		
		if ($pdo=PDO_DB_Conn::$PDOs) {
			
			$rs=$pdo->prepare($this->sql);
   	  	      	  	
   	  	    return $rs->execute(); 
			
		}else {
			
			return false;
		}		
		
	}
	/**
	 * 读取数据
	 * @see IModel::Read()
	 */
	function Read($DataArray){   
   	  new PDO_DB_Conn();
   	  
   	  if ($pdo=PDO_DB_Conn::$PDOs) {  
   	  	$find=" * ";
   	  	$where=null;
   	  	$order=null;
   	  	$limit=null;
   	  	
   	  	if (array_key_exists("find", $DataArray)){
   	  		$find="";
   	  	   foreach ($DataArray["find"] as $key=>$value) {
   	  		
   	  		 $find.=",".$value;
   	  		
   	  	    }
   	  	    $find=trim($find,",");
   	  	}
   	  	
   	  	
   	  	if (array_key_exists("where", $DataArray)){
   	  		foreach ($DataArray["where"] as $key=>$value) {
   	  		
   	  		  $where.=",".$value;
   	  		
   	  	    }
   	  	    $where=" where ".trim($where,","); 
   	  	} 

   	  	if (array_key_exists("order", $DataArray)){
   	  		foreach ($DataArray["order"] as $key=>$value) {
   	  			$order.=",".$value;
   	  		}
   	  		$order=" order by ".trim($order,",");
   	  		
   	  	}
   	  	if (array_key_exists("limit", $DataArray) and $DataArray["count"]==false){
   	  		$limit=" limit ".$DataArray["limit"]; 	
   	  	}
   	  	
   	  	if (!$this->sql){
   	  		
   	  		$this->sql="select ".$find." from ".$this->TableName.$where.$order.$limit;
   	  	}
   	  	
   	  	$pdo->query("set names utf8");    	  	
   	  	
   	  	$rs=$pdo->prepare($this->sql);
   	  
   	  	$rs->execute();   	  	
   	    
   	  	if ($DataArray["count"]==true){
   	  		//var_dump($rs->rowCount());
   	  		return count($rs->fetchAll());
   	  	}else {
   	  		return  $rs->fetchAll();
   	  	}
   	  		
   	  }else {
   	  	return false;
   	  }   	  
   	  
	}
	/**
	 * 删除数据
	 * Enter description here ...
	 */
	function Delete($DataArray){
		
		new PDO_DB_Conn();
		
   	    if ($pdo=PDO_DB_Conn::$PDOs) {
   	          
    	  if (@count($DataArray)){    	  	
   	  		   
   	  	       $where=" WHERE ".$DataArray["where"];

   	  	       $this->sql="DELETE FROM {$this->TableName} ".$where;
   	  	       
   	  	       if (@$DataArray["truncate"]==true){
   	  	       	   
   	  	       	   $this->sql="TRUNCATE TABLE ".$this->TableName;   	  	       	
   	  	       }   	  	       
   	  	       
    	   }	    	
   	    	
   	       $rs=$pdo->prepare($this->sql);
   	  	
   	  	   return $rs->execute();   
   	    	
   	    }else {
   	    	return false;
   	    }
	}
	
	
}
?>